package sge.data;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import sge.beans.AreaProfissional;

public class AreaProfissionalDAO {

	private static AreaProfissionalDAO dao;

	public AreaProfissionalDAO() {
	}

	public static AreaProfissionalDAO obterInstancia() {
		if (dao == null) {
			dao = new AreaProfissionalDAO();
		}

		return dao;
	}

	public void cadastrarAreaProfissional(AreaProfissional area) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			pm.makePersistent(area);
		} finally {
			pm.close();
		}
	}

	@SuppressWarnings("unchecked")
	public List<AreaProfissional> listarAreaProfissional() {

		List<AreaProfissional> lista = new ArrayList<AreaProfissional>();
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			Query query = pm.newQuery(AreaProfissional.class);
			lista.addAll((List<AreaProfissional>) query.execute());
		} finally {
			pm.close();
		}
		return lista;
	}

	@SuppressWarnings("unchecked")
	public Long buscarAreaProssional(String area) {
		List<AreaProfissional> lista = new ArrayList<AreaProfissional>();
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			Query query = pm.newQuery(AreaProfissional.class);
			lista.addAll((List<AreaProfissional>) query.execute());
		} finally {
			pm.close();
		}
		for (int i = 0; i < lista.size(); i++) {
			if (lista.get(i).getNome().equalsIgnoreCase(area)) {
				return lista.get(i).getIdAreaProfissional();
			}
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public Long buscarIDArea(String area) {
		List<AreaProfissional> lista = new ArrayList<AreaProfissional>();
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			Query query = pm.newQuery(AreaProfissional.class);
			lista.addAll((List<AreaProfissional>) query.execute());
		} finally {
			pm.close();
		}
		for (int i = 0; i < lista.size(); i++) {
			System.out.println(lista.get(i).getNome());
			if (lista.get(i).getNome().equalsIgnoreCase(area)) {
				return lista.get(i).getIdAreaProfissional();
			}
		}
		return null;
	}

}
